package com.leeon.container.map;

import net.mindview.util.TextFile;

import java.util.*;

import static net.mindview.util.Print.print;

class MapStorage{
    private String word;
    private int count;

    public MapStorage(String word){
        this.word = word;
        this.count = 1;
    }

    public String getWord() {
        return word;
    }

    public void add(){
        this.count++;
    }

    public int getCount(){
        return this.count;
    }

    @Override
    public String toString() {
        return "[" + word  + " = " + count + "]";
    }
}

public class Exmaple20 {
    public static void main(String[] args){
        List<String> ls=new ArrayList<String>(new TextFile("SetOperations.java","\\W+"));
        Collections.sort(ls,String.CASE_INSENSITIVE_ORDER);
        Set<MapStorage> mapStorageSet = new LinkedHashSet<MapStorage>();

        for (String s: ls) {
            boolean flags = false;
            Iterator<MapStorage> it = mapStorageSet.iterator();
            while (it.hasNext()){
                MapStorage mapStorage = it.next();
                if(mapStorage.getWord().equals(s)){
                    flags = true;
                    mapStorage.add();
                }
            }
            if(!flags){
                mapStorageSet.add(new MapStorage(s));
            }
        }

        print(mapStorageSet);
    }
}
